package com.shop.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.shop.bean.HitBean;
import com.shop.form.UserForm;
import com.shop.idao.IProductDao;
import com.shop.idao.IProductTypeDao;
import com.shop.idao.ISiteHitDao;
import com.shop.idao.IUserDao;
import com.shop.model.SiteHit;
import com.shop.model.User;

public class LoginAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user2 = (User) request.getSession().getAttribute("user");
		if (user2 != null) {
			request.getSession().removeAttribute("user");
			ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-*.xml");
			IProductTypeDao productTypeDao = (IProductTypeDao) app.getBean("productTypeDaoProxy");
			IProductDao productDao = (IProductDao) app.getBean("productDaoProxy");
			IUserDao userDao = (IUserDao) app.getBean("userDaoProxy");
			ISiteHitDao siteHitDao = (ISiteHitDao) app.getBean("siteHitDaoProxy");
			SiteHit siteHit = (SiteHit) app.getBean("siteHit");
			List productTypeList = productTypeDao.queryProductType();
			request.setAttribute("productTypeList", productTypeList);
			List productList = productDao.queryAllProductLimit();
			request.setAttribute("productList2", productList);
			List saleCountList = productDao.queryProductByProductSaleCount();
			request.setAttribute("saleCountList", saleCountList);
			List proList = productDao.queryAllProduct();
			request.setAttribute("proList", proList);
			List userList = userDao.queryALLUserByMemberScore();
			request.setAttribute("userList", userList);
			siteHit = siteHitDao.queryHit();
			String s_hit = siteHit.getHit() + "";
			char arrHit[] = s_hit.toCharArray();
			List hitList = new ArrayList();
			for (int i = 0; i < arrHit.length; i++) {
				HitBean hitbean = new HitBean();
				hitbean.setLength(arrHit.length);
				hitbean.setArrHit(arrHit[i]);
				hitList.add(hitbean);
			}
			request.setAttribute("hitList", hitList);
			siteHit.setHit(siteHit.getHit() + 1);
			siteHitDao.modifySiteHit(siteHit);
			return mapping.findForward("tuichu");
		}
		UserForm uf = (UserForm) form;
		String username = uf.getUsername();
		System.out.println(username);
		String password = uf.getPassword();
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-*.xml");
		IUserDao userDao = (IUserDao) app.getBean("userDaoProxy");
		User user = (User) app.getBean("user");
		IProductTypeDao productTypeDao = (IProductTypeDao) app.getBean("productTypeDaoProxy");
		IProductDao productDao = (IProductDao) app.getBean("productDaoProxy");
		ISiteHitDao siteHitDao = (ISiteHitDao) app.getBean("siteHitDaoProxy");
		SiteHit siteHit = (SiteHit) app.getBean("siteHit");
		boolean flag = userDao.queryUserByNameAndPass(username, password);
		if (flag == true) {
			user = userDao.queryUserByName(username);
			request.getSession().setAttribute("user", user);
			List productTypeList = productTypeDao.queryProductType();
			request.setAttribute("productTypeList", productTypeList);
			List productList = productDao.queryAllProductLimit();
			request.setAttribute("productList2", productList);
			List saleCountList = productDao.queryProductByProductSaleCount();
			request.setAttribute("saleCountList", saleCountList);
			List proList = productDao.queryAllProduct();
			request.setAttribute("proList", proList);
			List userList = userDao.queryALLUserByMemberScore();
			request.setAttribute("userList", userList);
			siteHit = siteHitDao.queryHit();
			String s_hit = siteHit.getHit() + "";
			char arrHit[] = s_hit.toCharArray();
			List hitList = new ArrayList();
			for (int i = 0; i < arrHit.length; i++) {
				HitBean hitbean = new HitBean();
				hitbean.setLength(arrHit.length);
				hitbean.setArrHit(arrHit[i]);
				hitList.add(hitbean);
			}
			request.setAttribute("hitList", hitList);
			siteHit.setHit(siteHit.getHit() + 1);
			siteHitDao.modifySiteHit(siteHit);
			return mapping.findForward("success");
		} else {
			return mapping.findForward("failure");
		}
	}
}